package com.pigotech.ptwo.utils;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.os.Vibrator;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by ghb on 2018/3/16.
 */

public class UtilSystem {
    public static boolean isPhoneNum(String num) {
        Pattern p = Pattern
                .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(num);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }
    }

    public static void selectPhoto(Activity activity, int requestCode) {
        Intent i = new Intent();
        i.setType("image/*");
        i.setAction(Intent.ACTION_GET_CONTENT);
        activity.startActivityForResult(i, requestCode);
    }

    public static void selectCamera(Activity activity, int requestCode) {
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Uri uri = activity.getContentResolver().insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new ContentValues());
        i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
        activity.startActivityForResult(i, requestCode);
    }

    public static void selectCamera(Activity activity, String file,
                                    int requestCode) {
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(new File(file)));
        activity.startActivityForResult(i, requestCode);
    }

    /**
     * 获取不同分辨率下匹配的大小值
     *
     * @param context
     * @param screenWidth
     *            标准屏幕宽
     * @param screenHeight
     *            标准屏幕高
     * @param width
     *            标准宽
     * @param height
     *            标准高
     * @return int[0] 匹配宽 int[1] 匹配高
     */
    public static int[] getMatchSize(Context context, int screenWidth,
                                     int screenHeight, int width, int height) {
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        int sw = manager.getDefaultDisplay().getWidth();
        int sh = manager.getDefaultDisplay().getHeight();
        int rw = (int) (width * sw * 1.0 / screenWidth);
        int rh = (int) (height * sh * 1.0 / screenHeight);
        return new int[] { rw, rh };
    }

    public static int getScreenWidth(Activity activity) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);

        return dm.widthPixels;
    }

    public static int getScreenHeight(Activity activity) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);

        return dm.heightPixels;
    }

    /**
     * 手机震动
     *
     * @param context
     * @param milliseconds
     */
    public static void vibrate(Context context, int milliseconds) {
        Vibrator vibrator = (Vibrator) context
                .getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(milliseconds);
    }

    public static void sendSms(Context context, String phoneNum, String content) {
        if (phoneNum == null) {
            phoneNum = "";
        }

        if (content == null) {
            content = "";
        }

        Uri smsToUri = Uri.parse("smsto:" + phoneNum);
        Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
        intent.putExtra("sms_body", content);
        context.startActivity(intent);
    }

    public static void sendSmsForResult(Activity activity, String phoneNum,
                                        String content, int requestCode) {
        if (phoneNum == null) {
            phoneNum = "";
        }

        if (content == null) {
            content = "";
        }

        Uri smsToUri = Uri.parse("smsto:" + phoneNum);
        Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
        intent.putExtra("sms_body", content);
        activity.startActivityForResult(intent, requestCode);
    }

    public static void setAlarmWithBrocast(Context context, Intent intent,
                                           long time) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                intent, 0);
        AlarmManager alarmManager = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
    }

    public static void setAlarmWithService(Context context, int requestCode, Intent intent, long time) {
        PendingIntent pendingIntent = PendingIntent.getService(context, requestCode,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
    }

    public static void cancelAlarmWithService(Context context, int requestCode, Intent intent) {
        PendingIntent pendingIntent = PendingIntent.getService(context, requestCode,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
    }

    // 检查软件包是否已安装
    public static boolean isPackageAvilible(Context context, String packageName) {
        List<PackageInfo> pinfo = context.getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);// 获取所有已安装程序的包信息
        List<String> pName = new ArrayList<String>();
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                pName.add(pn);
            }
        }
        return pName.contains(packageName);
    }

    public static String getSDPath(){
        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
